<?php


class Core_Db_Mysql implements IDb
{

    protected static $_instance = null;

    private $_params = array();


    private function  __construct($db_host,$db_users,$db_password,$db_name)
    {
        $this->_params['db_host'] = $db_host;
        $this->_params['db_users'] = $db_users;
        $this->_params['db_password'] = $db_password;
        $this->_params['db_name'] = $db_name;
    }

    public static function instance($db_host,$db_users,$db_password,$db_name)
    {
        if (null === self::$_instance) {
            self::$_instance = new self($db_host,$db_users,$db_password,$db_name);
        }
        return self::$_instance;
    }

    public function close()
    {
        mysql_close();
    }

    public function connect()
    {
        $this->_params['db_connect'] = mysql_connect($this->_params['db_host'],
                $this->_params['db_users'],$this->_params['db_password']);
        if ($this->_params['db_connect']) {
            mysql_query("SET NAMES utf8");
            mysql_select_db($this->_params['db_name']);
            return true;
        }
        return false;

    }

    public function query($query)
    {

        if (!$this->_params['db_connect']) {
            return false;
        }
        $result = mysql_query($query);
        $return = array();
        
        if(gettype($result) == 'boolean'){
        	return $return;
        }
        if ($result) {
            while ($row=mysql_fetch_assoc($result)) {
              $return[] = $row;
            }
            return $return;
        } else {
            return false;
        }
    }

    public function call_function($function, $params = array())
    {
        return call_user_func_array($function, $params);
    }

}
?>
